package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.mapper.UserMapper;
import com.neusoft.elmboot.util.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.UserService;
import com.neusoft.elmboot.util.APIResponse;

import javax.annotation.Resource;

@RestController
@RequestMapping("/User")
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping("/ByIdByPass")
	public APIResponse getUserByIdByPass(User user) {

		return APIResponse.success(userService.getUserByIdByPass(user));
	}

	@GetMapping("/ById")
	public APIResponse getUserById(User user) throws Exception {
		return APIResponse.success(userService.getUserById(user.getUserId()));
	}

	@PostMapping
	public APIResponse saveUser(User user) throws Exception {
		return APIResponse.success(userService.saveUser(user));
	}

	@GetMapping("/checkExist")
	public APIResponse checkExist(String id){
		try {
			User ret = userService.getUserById(id);
			return APIResponse.success(ret!=null);
		}catch (Exception e){
			return APIResponse.error(ErrorCode.ERROR);
		}
	}

	@Resource
	UserMapper userMapper;

	@GetMapping("/all")
	public APIResponse getUser(){
		try {
			return APIResponse.success(userMapper.getAll()) ;
		}catch (Exception e){
			return APIResponse.error(ErrorCode.ERROR);
		}
	}

	@DeleteMapping
	public APIResponse deleteUser(Integer id){
		try {
			return APIResponse.success(userMapper.deleteById(id));
		}catch (Exception e){
			return APIResponse.error(ErrorCode.ERROR);
		}
	}

}